package edu.geo.filters;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.vividsolutions.jts.geom.Coordinate;

import edu.geo.containers.GeoJson;
import edu.geo.containers.Json;
import edu.geo.containers.geom.impl.LineSegment;
import edu.geo.containers.geom.impl.Point;
import edu.geo.containers.geomdomain.GeometricDomain;
import edu.geo.db.conn.DBConnection;
import edu.geo.db.conn.DBConnectionProvider;
import edu.geo.exception.GeoInfoException;
import edu.geo.protocol.Properties;
import edu.geo.service.IGeometricDomainServiceFacade;

public class GreenYaoProcess extends GeoProcessDecorator {

	private static final Log LOGGER = LogFactory.getLog(GreenYaoProcess.class);

	private static final String GEODB = "geodb";
	private IGeometricDomainServiceFacade geomDomainService;

	private DBConnectionProvider dbConnectionProviderGeoDB = new DBConnection(
			GEODB);

	/*
	 * Ejemplo de petición de recuperacion de elementosa justados usando greenYao
	
	
	{
    "type": "Operation",
    "name": "greenYaoAdjustment",
    "properties": {
        "user": "geoinfouser",
        "domainId": "10"
    }
    }
	 */

	public GreenYaoProcess(IGeometricDomainServiceFacade geomDomainService) {
		super();
		this.geomDomainService = geomDomainService;
	}

	@Override
	public Json dispatchResult(Json requestParams) 
	{
		String domainIdStr = requestParams.getProperties().getDomainId();
		int domainId = Integer.parseInt(domainIdStr);
		LOGGER.info("Green yao adjustment whit domainid "+domainId);
		Json jsonFeatures= new Json(); 	
		try 
		{
			GeometricDomain domain = this.geomDomainService.retrieve(domainId);
			GeometricDomain features = this.geomDomainService.adjust(domain,false);
			List<LineSegment> lineSegments = features.getLineSegments();
			List<Point> points = features.getPoints();	
			
     		jsonFeatures.setName("GreenYaoAdjustmentData");
			jsonFeatures.setType("FeatureCollection");
			ArrayList<Json> featureList= new ArrayList<Json>();
			
			 if(points!=null)
		      {
		    	  for(Point point:points)
		    	  {
		    		 Json feature = new Json();
		    		 feature.setType("Feature");
		    		 GeoJson geoJson= new GeoJson();
					 geoJson.setType("Point");
					 ArrayList<Double> coordinates = new ArrayList<Double>();
					 coordinates.add(point.getX());
					 coordinates.add(point.getY());
					 geoJson.setCoordinates(coordinates);
					 feature.setGeometry(geoJson);
					 Properties prp = new Properties();
					 prp.setId(point.getId()+"");
					 prp.setResult(""+point.getAsWKT());
					 feature.setProperties(prp);
					 featureList.add(feature);				
		    	  }

		      }
		      
		      if(lineSegments!=null)
		      {
		    	  for(LineSegment line:lineSegments)
		    	  {
		    		  Json feature= new Json();
					  feature.setType("Feature");
						GeoJson geoJson= new GeoJson();
						geoJson.setType("LineString");
						ArrayList<ArrayList<Double>> coordinates = new ArrayList<ArrayList<Double>>();
						Coordinate[] coords = line.getCoordinates();
						int numCoords=coords.length;
						for(int i =0;i<numCoords;i++)
						{
							ArrayList<Double> coord= new ArrayList<Double>();
							coord.add(coords[i].x);
							coord.add(coords[i].y);
							coordinates.add(coord);
						}
						geoJson.setCoordinates(coordinates);
						feature.setGeometry(geoJson);
						Properties prp = new Properties();
						prp.setId(""+line.getId());
						prp.setResult(""+line.getAsWKT());
						feature.setProperties(prp);
						featureList.add(feature);			
		    	  }
		    	  
		      }
		      jsonFeatures.setFeatures(featureList);
			
			
		} catch (GeoInfoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsonFeatures;
	}

}
